package com.itic.mall.system.project.commission.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.mall.system.inter.project.commission.persistence.dao.ProjectCommissionDao;
import com.itic.mall.system.inter.project.commission.persistence.model.ProjectCommission;
import com.itic.mall.system.inter.project.commission.persistence.model.ProjectCommissionExample;
import com.itic.mall.system.inter.project.commission.persistence.model.ProjectCommissionExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class ProjectCommissionService
     extends CrudService<ProjectCommissionDao,ProjectCommission,ProjectCommissionExample> {
    @Autowired
    private ProjectCommissionDao projectCommissionDao;
    
    /***
     * 获取后台佣金百分比
     * @param level
     * @param userType
     * @param teamCount
     * @return
     */
    public ProjectCommission getCommissionByLevelAndUserTypeAndTeamCount(String level,String userType,int teamCount,String platForm) {
    	ProjectCommissionExample example = new ProjectCommissionExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andIsOpenEqualTo(1);
    	criteria.andLevelEqualTo(level);
    	criteria.andUserTypeEqualTo(userType);
    	criteria.andTeamCountLessThan(teamCount);
    	criteria.andPlatformEqualTo(platForm);
    	example.setOrderByClause("TEAM_COUNT desc");
    	List<ProjectCommission> projectCommissions = this.list(example);
    	if(projectCommissions.isEmpty())
    		return null;
    	else
    		return projectCommissions.get(0);
    }
    
    /**
     * 计算用户获取的佣金
     * @param projectCommission
     * @param totalCommission
     * @return
     */
    public String getUserCommission(ProjectCommission projectCommission,Double totalCommission) {
    	Double projectPercent = projectCommission.getPercent();
		String percent = NumberStringHelper.divString(projectPercent.toString(), "100", 2);
		String userCommission = NumberStringHelper.mulString(totalCommission.toString(), percent);

		return userCommission; 
    }

}